/******
 * waitpid.c - Simple wait usage
 *********/
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <errno.h>

#define MAX 5

int comp(const void *s1, const void *s2)
{
  printf("[(%s) (%s)]\n", *(char **) s1, *(char **) s2);
  return (strcmp(*(char **) s1, *(char **) s2));
}

int main(void)
{
  char *data[MAX], buf[80], *ptr, *key;
  int count;

  printf("Enter %d words, pressing Enter after each.\n", MAX);

  for (count = 0; count < MAX; count++)
  {
    printf("Word %d: ", count + 1);
    buf[0] = 97 + count;
    buf[1] = 0;
    data[count] = malloc(strlen(buf) + 1);
    strcpy(data[count], buf);
  }

  qsort(data, MAX, sizeof(data[0]), comp);

  for (count = 0; count < MAX; count++)
  {
    printf("\n%d: %s", count + 1, data[count]);
  }

  printf("\n\nEnter a search Key: ");
  gets(buf);

  key = buf;

  ptr = bsearch(&key, data, MAX, sizeof(data[0]), comp);

  if (ptr != NULL )
  {
    printf("%s found.\n", buf);
  }
  else
  {
    printf("%s not found.\n", buf);
  }
  return 0;

//  FILE *fp = fopen("", "r");
//  if (!fp)
//  {
//    perror("aa\n");
//    printf("%d", errno);
//  }

//  printf("test assert()\n");
//  int a = 0;
//  assert(a > 9);

  return 0;
}
